﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using EcoSystem.Permissions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Volo.Abp;
using Volo.Abp.Application.Dtos;

namespace EcoSystem;

/// <summary>
/// HTTP - API接口管理
/// </summary>
[Area(EcoSystemRemoteServiceConsts.ModuleName)]
[RemoteService(Name = EcoSystemRemoteServiceConsts.RemoteServiceName)]
[Route("api/Platform/httpApiConfig")]
public class HttpApiConfigController : EcoSystemController, IHttpApiConfigAppService
{
    private readonly IHttpApiConfigAppService _service;

    public HttpApiConfigController(IHttpApiConfigAppService service)
    {
        _service = service;
    }

    /// <summary>
    /// 创建
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<HttpApiConfigDto> CreateAsync(CreateOrUpdateHttpApiConfigDto input)
    {
        return await _service.CreateAsync(input);
    }

    /// <summary>
    /// 获取详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("{id}")]
    public async Task<HttpApiConfigDto> GetAsync(Guid id)
    {
        return await _service.GetAsync(id);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="id"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("{id}")]
    public async Task<HttpApiConfigDto> UpdateAsync(Guid id, CreateOrUpdateHttpApiConfigDto input)
    {
        return await _service.UpdateAsync(id, input);
    }


    /// <summary>
    /// 列表查询
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<PagedResultDto<HttpApiConfigDto>> GetListAsync(GetHttpApiConfigInput input)
    {
        return await _service.GetListAsync(input);
    }

    /// <summary>
    /// 获取所有
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("all")]
    public async Task<ListResultDto<HttpApiConfigDto>> GetAllAsync(GetHttpApiConfigInput input)
    {
        return await _service.GetAllAsync(input);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("{id}")]
    public async Task DeleteAsync(Guid id)
    {
        await _service.DeleteAsync(id);
    }

    [HttpGet]
    [Route("owns/list")]
    public async Task<ListResultDto<HttpApiConfigDto>> GetOwnsListAsync(bool onlyEnabled = true)
    {
        return await _service.GetOwnsListAsync(onlyEnabled);
    }

    [HttpGet]
    [Route("config/{code}")]
    public Task<HttpApiConfigDto> GetForCodeAsync(string code)
    {
        return _service.GetForCodeAsync(code);
    }

    /// <summary>
    /// 查询数据
    /// </summary>
    /// <param name="code"></param> 
    /// <returns></returns>
    [HttpPost]
    [HttpGet]
    [Route("query/{code}")]
    [Route("/api/report/query/{code}")]
    [AllowAnonymous]
    public async Task<object> QueryDataAsync(string code)
    {
        if (Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
        {
            StreamReader stream = new StreamReader(Request.Body);
            string body = stream.ReadToEnd();
            var input = JsonConvert.DeserializeObject<DictionaryRequestInputDto>(body);
            return await _service.QueryDataAsync(code, input);
        }
        else
        {
            DictionaryRequestInputDto input = new DictionaryRequestInputDto();
            foreach (var kv in Request.Query)
            {
                input.Add(kv.Key, kv.Value);
            }
            return await _service.QueryDataAsync(code, input);
        }
    }

    [NonAction]
    public async Task<object> QueryDataAsync(string code, DictionaryRequestInputDto input)
    {
        return await _service.QueryDataAsync(code, input);
    }

    /// <summary>
    /// 查询导出Excel
    /// </summary>
    /// <param name="code"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("query/{code}/export")]
    [Route("/api/report/query/{code}/export")]
    public async Task<object> ExportDataAsync(string code, ExportExcelInputDto input)
    {
        var result = await _service.ExportDataAsync(code, input) as byte[];
        return File(result, "application/ms-excel", $"导出【{code}】{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx");
    }
}
